package com.hawthorn.admin.service.syslog.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.repository.SysLogMapper;
import com.hawthorn.admin.service.syslog.SysLogService;
import com.hawthorn.component.constant.FormatConstant;
import com.hawthorn.platform.service.ExcelApiService;
import com.hawthorn.platform.service.TransResultService;
import com.hawthorn.platform.utils.common.ArrayMyUtil;
import com.hawthorn.platform.utils.common.MyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@BelongsProject: hawthorn-admin
 *@BelongsPackage: com.hawthorn.admin.service.syslog.impl
 *@Author: 郭昊：guohao
 *@CreateTime: 2022-07-15  09:47
 *@Description: TODO
 *@Version: 1.0
 */
@Service
@Slf4j
public class SysLogServiceImpl implements SysLogService
{

  @Resource
  private SysLogMapper sysLogMapper;

  @Resource
  private TransResultService transResultService;

  @Resource
  private ExcelApiService excelApiService;

  /**
   * <p>todo 获取用户登录日志</p>
   * @param params
   * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/4 15:25
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/4 guohao v1.0.1 init
   */
  @Override
  public IPage<Map<String, Object>> findLogInOperateLog(String params)
  {
    Page<Map<String, Object>> page = new Page<>();

    String conditions = MyUtil.getConditionsWhere(params, page);
    // Long userId = ContextHolder.getUserId();
    // conditions += " and user_id ='" + userId + "'";
    // 绑定字典
    HashMap<String, String> transHash = new HashMap<>();
    transHash.put("deptId", "dept|o");
    transHash.put("loginTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    transHash.put("logoutTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    return transResultService.transResult(sysLogMapper.selectLogInOperateLog(conditions, page), transHash);
  }

  /**
   * <p>todo 获取用户操作日志</p>
   * @param params
   * @return: com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map < java.lang.String, java.lang.Object>>
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/7 16:06
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/7 guohao v1.0.1 init
   */
  @Override
  public IPage<Map<String, Object>> findOperationLog(String params)
  {
    Page<Map<String, Object>> page = new Page<>();
    String conditions = MyUtil.getConditionsWhere(params, page);

    // 绑定字典
    HashMap<String, String> transHash = new HashMap<>();
    transHash.put("userDeptId", "dept|o");
    transHash.put("createTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    IPage<Map<String, Object>> operationLists = sysLogMapper.selectOperationLog(conditions, page);
    List<Map<String, Object>> dataList = operationLists.getRecords();
    for (Map<String, Object> map : dataList)
    {
      map.put("execTime", map.get("execTime").toString() + "ms");
    }
    operationLists.setRecords(dataList);
    return transResultService.transResult(sysLogMapper.selectOperationLog(conditions, page), transHash);
  }

  /**
   * <p>todo 用户操作日志导出</p>
   * @param response
   * @param fileName
   * @param params
   * @return: void
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/12 9:42
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/12 guohao v1.0.1 init
   */
  @Override
  public void exportOperationLog(HttpServletResponse response, String fileName, Map<String, Object> params)
  {
    String header = MyUtil.getValue(params.get("header"));
    String param = MyUtil.getValue(params.get("params"));
    String qc = MyUtil.getConditionsWhere(param, null);
    List<List<String>> headList = new ArrayList<>();
    List<String> headFields = excelApiService.toHeader(header, headList);
    String fields = ArrayMyUtil.join(headFields.toArray(), ",");
    List<Map<String, Object>> dataList = sysLogMapper.exportOperationLog(fields, qc);
    HashMap<String, String> transHash = new HashMap<>();
    transHash.put("deptId", "dept|o");
    transHash.put("createTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    excelApiService.export(response, fileName, headFields, headList, dataList, transHash);
  }

  /**
   * <p>todo 用户登录日志导出</p>
   * @param response
   * @param fileName
   * @param params
   * @return: void
   * @author: guohao | 2607728024@qq.com
   * @date: 2022/7/12 9:53
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/12 guohao v1.0.1 init
   */
  @Override
  public void exportLogInOperateLog(HttpServletResponse response, String fileName, Map<String, Object> params)
  {
    String header = MyUtil.getValue(params.get("header"));
    String param = MyUtil.getValue(params.get("params"));
    String qc = MyUtil.getConditionsWhere(param, null);
    List<List<String>> headList = new ArrayList<>();
    List<String> headFields = excelApiService.toHeader(header, headList);
    String fields = ArrayMyUtil.join(headFields.toArray(), ",");
    List<Map<String, Object>> dataList = sysLogMapper.exportLogInOperateLog(fields, qc);
    HashMap<String, String> transHash = new HashMap<>();
    transHash.put("deptId", "dept|o");
    transHash.put("loginTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    transHash.put("logoutTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    excelApiService.export(response, fileName, headFields, headList, dataList, transHash);
  }
}
